<?php
require __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../Business/Applications/Game/Autoloader.php';
require __DIR__ . '/../global_config.php';
require(__DIR__ . '/../Business/Applications/Game/Core/System.func.php');
require(__DIR__ . '/funcs.php');

foreach (glob(__DIR__ . '/../Business/Applications/Game/Config/*.php') as $file) {
    include_once $file;
}
foreach (glob(__DIR__ . '/../Business/Applications/Game/Config/*/*.php') as $file) {
    include_once $file;
}

use \Workerman\Worker;
use \Workerman\WebServer;
use \Workerman\MySQL\Connection as Connection;
use \Workerman\Lib\Timer;

$manage_web = new WebServer('http://0.0.0.0:8082');
$manage_web->addRoot('www.admin.com', __DIR__ . '/');
$manage_web->name = 'Manage Web';
$manage_web->count = 2;

$global_config = GLOBAL_CONFIG;

\Workerman\Protocols\Http::sessionSavePath('/tmp');
WebServer::$logFile = __DIR__ . '/Log/stdout.log';
WebServer::$pidFile = GLOBAL_CONFIG['pid_file_path'] . '/' . str_replace('/', '_', __FILE__) . '.pid';

$manage_db = $game_redis = $logdb = $log_redis = NULL;

$manage_web->onWorkerStart = function ($worker) {
    global $game_redis, $manage_db, $logdb, $log_redis;

    $manage_db = new Connection(
        GLOBAL_CONFIG['db']['manage']['host'],
        GLOBAL_CONFIG['db']['manage']['port'],
        GLOBAL_CONFIG['db']['manage']['user'],
        GLOBAL_CONFIG['db']['manage']['password'],
        GLOBAL_CONFIG['db']['manage']['name']
    );

    $game_redis = new Redis();
    if (GLOBAL_CONFIG['redis']['game']['sock']) {
        $game_redis->connect(GLOBAL_CONFIG['redis']['game']['sock']);
    } else {
        $game_redis->connect(GLOBAL_CONFIG['redis']['game']['ip'], GLOBAL_CONFIG['redis']['game']['port']);
        if (GLOBAL_CONFIG['redis']['game']['password']) $game_redis->auth(GLOBAL_CONFIG['redis']['game']['password']);
    }

    $logdb = new Connection(
        GLOBAL_CONFIG['log_server']['mysql']['host'],
        GLOBAL_CONFIG['log_server']['mysql']['port'],
        GLOBAL_CONFIG['log_server']['mysql']['user'],
        GLOBAL_CONFIG['log_server']['mysql']['password'],
        GLOBAL_CONFIG['log_server']['mysql']['name'],
    );

    $log_redis = new Redis();
    if (GLOBAL_CONFIG['log_server']['redis']['sock']) {
        $log_redis->connect(GLOBAL_CONFIG['log_server']['redis']['sock']);
    } else {
        $log_redis->connect(GLOBAL_CONFIG['log_server']['redis']['host'], GLOBAL_CONFIG['log_server']['redis']['port']);
        if (GLOBAL_CONFIG['log_server']['redis']['password']) $log_redis->auth(GLOBAL_CONFIG['log_server']['redis']['password']);
    }
};

if (!defined('GLOBAL_START')) {
    Worker::runAll();
}